package GUILayer;
import CtrlLayer.*;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PopupOpretKunde extends PopupWindow
{
	private PersonCtrl PC;
	private JTextField txtErhvervskunde;
	private JTextField textNavn;
	private JTextField textAdresse;
	private JTextField textPostnr;
	private JTextField textBy;
	private JTextField textTelefon;
	private JTextField textEmail;

	public PopupOpretKunde(Updateable mother)
	{
		super(mother);
		PC = new PersonCtrl();
		getContentPane().setPreferredSize(new Dimension(440, 310));
		setTitle("Opret Kunde");
        
        JLabel lblNavn = new JLabel("Navn");
        JLabel lblAdresse = new JLabel("Adresse");
        JLabel lblPostnr = new JLabel("Postnr.");
        JLabel lblBy = new JLabel("By");
        JLabel lblTelefon = new JLabel("Telefon");
        JLabel lblEmail = new JLabel("Email");
        JLabel lblErhvervskunde = new JLabel("Erhvervskunde rabat");
        
        JButton btnAnnuller = new JButton("Annuller");
        btnAnnuller.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)
        	{
        		quit();
        	}
        });
        JButton btnOpret = new JButton("Opret");
        btnOpret.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		opretKunde();
        		getMother().update();
				quit();
        	}
        });
        
        JButton btnRydFelter = new JButton("Ryd Felter");
		btnRydFelter.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				clearFields();
			}
		});
        
        textNavn = new JTextField();
        textNavn.setColumns(10);
        
        textAdresse = new JTextField();
        textAdresse.setColumns(10);
        
        textPostnr = new JTextField();
        textPostnr.setColumns(10);
        
        textBy = new JTextField();
        textBy.setColumns(10);
        
        textTelefon = new JTextField();
        textTelefon.setColumns(10);
        
        textEmail = new JTextField();
        textEmail.setColumns(10);
        
        txtErhvervskunde = new JTextField();
        txtErhvervskunde.setColumns(10);
        
        JButton btnNewButton = new JButton("Ryd Felter");
           
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
        					.addGap(28)
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addComponent(lblErhvervskunde)
        						.addComponent(lblNavn)
        						.addComponent(lblAdresse)
        						.addComponent(lblPostnr)
        						.addComponent(lblBy)
        						.addComponent(lblTelefon, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
        						.addComponent(lblEmail))
        					.addGap(25)
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addComponent(textEmail, 216, 216, 216)
        						.addComponent(textTelefon, 216, 216, 216)
        						.addComponent(textBy, 216, 216, 216)
        						.addComponent(textPostnr, 216, 216, 216)
        						.addComponent(textAdresse, 216, 216, 216)
        						.addComponent(textNavn, 216, 216, 216)
        						.addComponent(txtErhvervskunde, GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)))
        				.addGroup(groupLayout.createSequentialGroup()
        					.addContainerGap()
        					.addComponent(btnNewButton)
        					.addGap(18)
        					.addComponent(btnAnnuller)
        					.addGap(18)
        					.addComponent(btnOpret)))
        			.addContainerGap(24, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblNavn)
        				.addComponent(textNavn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblAdresse)
        				.addComponent(textAdresse, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblPostnr)
        				.addComponent(textPostnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblBy, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textBy, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblTelefon)
        				.addComponent(textTelefon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblEmail)
        				.addComponent(textEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblErhvervskunde)
        				.addComponent(txtErhvervskunde, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(btnOpret)
        				.addComponent(btnAnnuller)
        				.addComponent(btnNewButton))
        			.addContainerGap())
        );
        
        getContentPane().setLayout(groupLayout);
        pack();
	}
	
	private void opretKunde()	//indlaeser text fra skaermen
    {
        String name = textNavn.getText();
        String address = textAdresse.getText();
        String postal = textPostnr.getText();
        String city = textBy.getText();
        String phone = textTelefon.getText();
        String email = textEmail.getText();
        int craftsmanDiscount = Integer.parseInt(txtErhvervskunde.getText());
        PC.createCustomer(name, address, postal, city, phone, email, craftsmanDiscount);
    }
	
	public void clearFields()
	{
		textNavn.setText(null);
		textAdresse.setText(null);
		textPostnr.setText(null);
		textBy.setText(null);
		textTelefon.setText(null);
		textEmail.setText(null);
    	txtErhvervskunde.setText(null);
	}
}
